/*
 * @Author: szx
 * @Date: 2022-03-22 13:48:35
 * @LastEditTime: 2022-03-22 13:51:46
 * @Description:
 * @FilePath: \leetcode\剑指offer\32\32.js
 */
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
var levelOrder = function (root) {
    if (!root) return [];
    const res = [];
    let que = [root];
    let flag = false;
    while (que.length) {
        const tmp = [];
        const tmp_que = [];
        for (let i = 0; i < que.length; i++) {
            tmp.push(que[i].val);
            if (que[i].left) tmp_que.push(que[i].left);
            if (que[i].right) tmp_que.push(que[i].right);
        }
        que = tmp_que;
        if (flag == false) {
            res.push(Array.from(tmp).reverse());
        } else res.push(Array.from(tmp));
        flag = ~flag;
    }
    return res;
};
